﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Nop.Core.Domain.Common;
using Nop.Services.Customers;
using Nop.Services.Helpers;
using Nop.Services.Localization;
using Nop.Services.ShareBuy;
using Nop.Core.Domain.ShareBuy;
using Nop.Services.Security;
using Nop.Web.Framework;
using Nop.Web.Framework.Controllers;
using Telerik.Web.Mvc;
using Nop.Admin.Models.ShareBuy;

namespace Nop.Admin.Controllers
{
	[AdminAuthorize]
    public class ShareBuyController : BaseNopController
	{
		#region Fields

        private readonly IShareBuyService _shareBuyService;
        private readonly ILanguageService _languageService;
        private readonly IDateTimeHelper _dateTimeHelper;
        private readonly ILocalizationService _localizationService;
        private readonly IPermissionService _permissionService;
        private readonly AdminAreaSettings _adminAreaSettings;
        
		#endregion

		#region Constructors

        public ShareBuyController(IShareBuyService shareBuyService, ILanguageService languageService,
            IDateTimeHelper dateTimeHelper, 
            ILocalizationService localizationService, IPermissionService permissionService,
            AdminAreaSettings adminAreaSettings)
        {
            this._shareBuyService = shareBuyService;
            this._languageService = languageService;
            this._dateTimeHelper = dateTimeHelper;
            this._localizationService = localizationService;
            this._permissionService = permissionService;
            this._adminAreaSettings = adminAreaSettings;
		}

		#endregion 
        
        #region News items

        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

        public ActionResult List()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageNews))
                return AccessDeniedView();

            var shares = _shareBuyService.GetShareBuys(0, null, 0, _adminAreaSettings.GridPageSize, true);
            var gridModel = new GridModel<ShareBuyItemModel>
            {
                Data = shares.Select(x =>
                {
                    var m = x.ToModel();
                    m.CreatedOnUtc = _dateTimeHelper.ConvertToUserTime(x.CreatedOnUtc, DateTimeKind.Utc);
                    m.Comments = x.ShareBuyComments.Count;
                    return m;
                }),
                Total = shares.TotalCount
            };
            return View(gridModel);
        }

        [HttpPost, GridAction(EnableCustomBinding = true)]
        public ActionResult List(GridCommand command)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageNews))
                return AccessDeniedView();

            var news = _shareBuyService.GetShareBuys(0, null, command.Page - 1, command.PageSize, true);
            var gridModel = new GridModel<ShareBuyItemModel>
            {
                Data = news.Select(x =>
                {
                    var m = x.ToModel();
                    m.CreatedOnUtc = _dateTimeHelper.ConvertToUserTime(x.CreatedOnUtc, DateTimeKind.Utc);
                    m.Comments = x.ShareBuyComments.Count;
                    return m;
                }),
                Total = news.TotalCount
            };
            return new JsonResult
            {
                Data = gridModel
            };
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageNews))
                return AccessDeniedView();

            var shareBuyItem = _shareBuyService.GetShareBuyById(id);
            if (shareBuyItem == null)
                //No shareBuyItem item found with the specified id
                return RedirectToAction("List");

            _shareBuyService.DeleteShareBuy(shareBuyItem);

            SuccessNotification("Sharebuy deleted");
            //return RedirectToAction("List");
            return Content("Success");
        }
        
        #endregion

    }
}
